

import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';


class PersonalInformation extends StatefulWidget {
  @override
  _PersonalInformationState createState() => _PersonalInformationState();
}

class _PersonalInformationState extends State<PersonalInformation> {

  var _image;
  List personalInformationTitles = [
    '会员名:',
    '昵称:',
    '性别:',
    '个性签名:',
  ];
  List personalInformationSubtitle = [
    'WZY',
    '王子异',
    '男',
    '我就是我，不一样的烟火',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("个人信息"),
      ),
      body: Container(
        child: ListView.separated(
            itemBuilder: (BuildContext context, int index) {
              if (index == 0) {
                return
                  GestureDetector(child: Container(
                    child: Column(
                      children: <Widget>[
                        Container(
                            margin: EdgeInsets.all(10),
                            width: 100,
                            height: 100,
                            child: ClipOval(
                              child:
                              _image!=null?
                             Image.file(_image,
                                fit: BoxFit.cover,
                              ):Image.asset(
                                  'assets/images/touxiang.png'),
                            )),
                        Container(
                            margin: EdgeInsets.only(bottom: 10),
                            child: Text("点击更换头像"))
                      ],
                    ),
                  ) ,onTap: () {
                    showModalBottomSheet(
                        context: context,
                        builder: (BuildContext context) {
                          return Column(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              ListTile(
                                title: Center(child: Text("拍照上传")),onTap: ()=>
                              _takePhoto(),
                              ),
                              Divider(
                                height: 10,
                              ),
                              ListTile(
                                title:  Center(child: Text("本地上传")),onTap: ()=>
                              _openGallery()
                                ),

                              Divider(
                                height: 20,
                                thickness: 10,
                              ),
                              ListTile(
                                title: Center(child: Text("取消")),
                                onTap: () => Navigator.pop(context, true),
                              ),
                            ],
                          );
                        });
                  });
              }
              index -= 1;
              return ListTile(
                title: Row(
                  children: <Widget>[
                Expanded(child: Text(personalInformationTitles[index]),flex: 1,),

                Expanded(child: Container(
                      child: Text(
                        personalInformationSubtitle[index],
                        style: TextStyle(color: Colors.grey),
                      ),
                    ),flex: 3,),
                  ],
                ),

              );
              index -= 1;
            },
            separatorBuilder: (context, index) {
              return Divider(height: 1);
            },
            itemCount: personalInformationTitles.length + 1),
      ),
    );
  }
/*拍照*/
  // 创建picker

  _takePhoto() async {
    var image = await ImagePicker.pickImage (source: ImageSource.camera);

    setState(() {
      _image = image ;
      Navigator.pop(context, true);
    });

  }

  /*相册*/
  _openGallery() async {
    var image =
    await ImagePicker.pickImage(source: ImageSource.gallery);

    setState(() {
      _image = image;
      Navigator.pop(context, true);
    });
  }
  }
